from flask import Flask, request, jsonify
from flask_cors import CORS
import sqlite3
import json
#按照视频搜索所有对应数据
def get_video_data(video_name):
    try:
        # 连接到SQLite数据库
        conn = sqlite3.connect('D:/VidBot/local/video_database.db')
        c = conn.cursor()

        # 查询视频数据
        c.execute("SELECT * FROM videos WHERE name=?", (video_name,))
        video_data = c.fetchone()

        # 关闭连接
        conn.close()

        # 如果找到对应视频，则返回数据
        if video_data:
            return {
                "id": video_data[0],
                "name": video_data[1],
                "tags": video_data[2],
                "storage_path": video_data[3],
                "nodejson": video_data[4],
                "edgejson": video_data[5],
                "treejson": video_data[6],
                "content": video_data[7],
                "summary": video_data[8]
            }
        else:
            print("Video not found!")
            return None
    except Exception as e:
        print("Error getting video data:", e)
        return None
    
#联查：输入node返回查到的视频id
def get_video_data_by_node(node_content):
    try:
        # 连接到SQLite数据库
        conn = sqlite3.connect('video_database.db')
        c = conn.cursor()

        # 查询节点对应的视频ID
        c.execute("SELECT video_id FROM link WHERE node=?", (node_content,))
        video_ids = c.fetchall()
        print(video_ids)
        # 存储视频数据
        video_data_list = []

        # 如果找到对应视频ID，则根据视频ID获取视频数据
        if video_ids:
            for video_id in video_ids:
                video_id = video_id[0]
                # 查询视频ID对应的视频数据
                video_data = get_video_data_by_id(video_id)
                if video_data:
                    video_data_list.append(video_data)

        # 关闭连接
        conn.close()

        # 返回视频数据列表
        return video_data_list

    except Exception as e:
        print("Error getting video data by node:", e)
        return None
    
def get_video_data_by_id(video_id):
    try:
        # 连接到SQLite数据库
        conn = sqlite3.connect('video_database.db')
        c = conn.cursor()

        # 查询视频数据
        c.execute("SELECT * FROM videos WHERE id=?", (video_id,))
        video_data = c.fetchone()

        # 关闭连接
        conn.close()

        # 如果找到对应视频，则返回数据
        if video_data:
            return {
                "name": video_data[1],
                "treejson": video_data[6],
            }
        else:
            print("Video not found!")
            return None
    except Exception as e:
        print("Error getting video data by ID:", e)
        return None

app=Flask(__name__)
CORS(app)

@app.route('/init',methods=['GET','POST'])
def init():
    data=request.get_json()
    video_name=data.get('videoname')
    video_data=get_video_data(video_name)
    return_data={
        'nodejson':video_data['nodejson'],
        'edgejson':video_data['edgejson'],
        'treejson':video_data['treejson'],
        'content':video_data['content'],
        'summary':video_data['summary']
        }
    return return_data 

@app.route('/liancha',methods=['GET','POST'])
def liancha():
    data=request.get_json()
    node_name=data.get('node')
    video_data=get_video_data_by_node(node_name)
    print(node_name)
    print(video_data)
    video_data={'video_data':video_data}
    
    return video_data

# 启动 Flask 应用程序
if __name__ == '__main__':
    app.run(debug=True,port=8090)
